package com.ling.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ling.bean.Msg;
import com.ling.bean.User;
import com.ling.bean.UserFollow;
import com.ling.service.UserFollowService;
import com.ling.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserFollowController {
    private final UserFollowService userFollowService;
    private final UserService userService;

    @Autowired
    public UserFollowController(UserService userService, UserFollowService userFollowService) {
        this.userService = userService;
        this.userFollowService = userFollowService;
    }

    //获取指定用户关注的账号
    @RequestMapping(value = "/followingUser/{userId}/{pn}",method = RequestMethod.GET)
    @ResponseBody
    public Msg getFollowingUsers(@PathVariable String userId,@PathVariable Integer pn) {
        PageHelper.startPage(pn, 10);
        List<User> followingUsers = userService.getFollowingUsers(userId);
        PageInfo<User> pageInfo = new PageInfo<>(followingUsers);
        return Msg.success().add("pageInfo",pageInfo);
    }


    @GetMapping("/getUserFollows/{pn}")
    @ResponseBody
    public Msg getUserFollowWithPage(HttpSession httpSession, @PathVariable Integer pn) {
        User user = (User) httpSession.getAttribute("user");
        if (user == null) {
            return Msg.fail().add("error", "请重新登录！");
        }//登陆判断
        PageHelper.startPage(pn, 10);
        List<UserFollow> userFollows = userFollowService.getUserFollows(user.getId());
        PageInfo<UserFollow> pageInfo = new PageInfo<>(userFollows);
        return Msg.success().add("pageInfo",pageInfo);
    }

}
